<?php

namespace app\service;

use think\facade\Lang;

class WxService
{
	public $AppID 		= "wx6e23e8ac3b9e32e1";
	public $AppSecret 	= "0601681e6af5617834b795583e9ba986";
	public $token 		= "";
	public $ticket 		= "";
	public $url 		= "";
	const POST_METHOD   = 'POST';
	const GET_PHONE_URL = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s';
	const ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';

	public function __construct()
	{
		if (isset($_SERVER['HTTP_HOST'])) {
			$this->url = "http://" . $_SERVER['HTTP_HOST'];
		}
	}

	//curl获取请求文本内容
	public function get_curl_contents($url, $method = 'GET', $data = array())
	{
		if ($method == self::POST_METHOD) {
			//使用crul模拟
			$ch = curl_init();
			//允许请求以文件流的形式返回
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
			curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 30);
			curl_setopt($ch, CURLOPT_URL, $url);
			$result = curl_exec($ch); //执行发送
			curl_close($ch);
		} else {
			$ch = curl_init();
			//允许请求以文件流的形式返回
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
			//禁用https
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_URL, $url);
			$result = curl_exec($ch); //执行发送
			curl_close($ch);
		}
		return $result;
	}
	/**
	 * curl post发送json格式 
	 */
	public function jsonPost($url, $data = [])
	{

		$curl = curl_init();

		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

		if (!$data) return false;
		if (is_array($data)) $data = json_encode($data);

		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json; charset=utf-8',
			'Content-Length:' . strlen($data),
			'Cache-Control: no-cache',
			'Pragma: no-cache'
		));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$res = curl_exec($curl);
		$errorno = curl_errno($curl);
		if ($errorno) {
			return $errorno;
		}
		curl_close($curl);
		return $res;
	}
	/**
	 * 获取微信公从号access_token 
	 */
	public function getAccessToken()
	{
		# $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->AppID.'&secret='.$this->AppSecret;
		$url = sprintf(self::ACCESS_TOKEN_URL, $this->AppID, $this->AppSecret);
		$res = $this->get_curl_contents($url);
		$res = json_decode($res, true);

		$this->token = isset($res['access_token']) ? $res['access_token'] : '';
	}
	/**
	 * 获取微信公从号ticket (网页版使用)
	 */
	public function getJsapiTicket($token)
	{
		$url = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", $token);
		$res = $this->get_curl_contents($url);
		$res = json_decode($res, true);

		$this->ticket = $res['ticket'];
	}
	/**
	 * 获取小程序 手机号
	 */
	public function getMiniPhone($data = [])
	{
		$this->getAccessToken();

		if (empty($this->token))    return false;

		$url = sprintf(self::GET_PHONE_URL, $this->token);

		$res = $this->jsonPost($url, $data);
		$phoneRes = json_decode($res, true);

		return $phoneRes;
	}
	/**
	 * 小程序通过 code获取openid 
	 */
	public function getOpenidByCode($code = '')
	{
		$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->AppID . '&secret=' . $this->AppSecret . '&js_code=' . $code . '&grant_type=authorization_code';

		$res = $this->get_curl_contents($url);
		$res = json_decode($res, true);

		return $res;
	}

	/**
	 * 发送订阅消息
	 *
	 * @param array $data
	 * 
	 * @return void
	 */
	public function messageSubscribeSend($data)
	{
		if (empty($this->token)) {
			$this->getAccessToken();
			if (empty($this->token)) {
				return false;
			}
		}
		$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $this->token;
		//$res = $this->get_curl_contents($url, self::POST_METHOD, json_encode($data));
		$res = $this->jsonPost($url, $data);
		
		$res = json_decode($res, true);

		return $res;
	}
}
